package com.yssoft.service.core;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.yssoft.entity.core.TSupplier;
import com.yssoft.entity.core.TSupplierLicence;
import com.yssoft.entity.core.TenterpriseLicence2;
import com.yssoft.mapper.core.TSupplierLicenceMapper;
import com.yssoft.mapper.core.TSupplierMapper;
import com.yssoft.shiro.MyRealm;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.UploadFile;
import com.yssoft.utils.UserHelper;

@Service
public class SupplierService {
	
	private static Logger logger=Logger.getLogger(SupplierService.class);

	@Resource
	private TSupplierMapper tSupplierMapper;
	@Resource
	private TSupplierLicenceMapper tSupplierLicenceMapper;
	
	/**
	 * 查询供应商列表
	 * @param pageNumber
	 * @param pageSize
	 * @param tSupplier
	 * @return
	 */
	@Transactional(rollbackFor=Exception.class)
	public List<Map<String,Object>> querySupplierList(Integer pageNumber, Integer pageSize, TSupplier tSupplier, HttpServletRequest request) {
		if(pageNumber!=null && pageSize!=null){
			int start = ((pageNumber - 1) * pageSize);
			int end = pageSize;
			tSupplier.setStart(start);
			tSupplier.setEnd(end);
		}
        tSupplier.settEId(AppUserHelper.getCurrentCompanyId(request));
        tSupplier.setDeleteFlag("0");
        List<Map<String,Object>> supplierList = tSupplierMapper.selectListSql(tSupplier);
		return supplierList;
	}

	/**
	 * 根据id查询供应商信息
	 * @param id
	 * @return
	 */
	public TSupplier selectByPrimaryKey(Integer id) {
		try {
            return tSupplierMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            logger.info("com.yssoft.service.core.SupplierService.selectByPrimaryKey根据供应商id查询供应商信息报错："+e.getMessage());
            return null;
        }
	}

	/**
	 * 供应商新增保存
	 * @param tSupplier
	 * @param tEnterpriseLicence
	 */
	public void saveSupplier(TSupplier tSupplier, TenterpriseLicence2 tEnterpriseLicence, HttpServletRequest request) {
		/*供应商信息实体补充基础数据*/
		tSupplier.setCreateTime(new Date());
        tSupplier.settEId(AppUserHelper.getCurrentCompanyId(request));
        tSupplier.setCreatorName(AppUserHelper.getCurrentUserName(request));
        tSupplier.setDeptName(AppUserHelper.getCurrentDeptName(request));
        tSupplier.setDeptId(AppUserHelper.getCurrentDeptId(request));
        tSupplier.setCreatorId(AppUserHelper.getCurrentUserId(request));
        tSupplier.setDeleteFlag("0");
		/*保存供应商信息*/
		tSupplierMapper.insertSelective(tSupplier);
		/*获取新增供应商信息id*/
		Integer id = tSupplier.getId();
		/*企业证照基础信息*/
        TSupplierLicence licence = new TSupplierLicence();
        licence.settSId(id);
        licence.setCreateTime(new Date());
        licence.setCreatorName(AppUserHelper.getCurrentUserName(request));
        licence.setCreatorId(AppUserHelper.getCurrentUserId(request));
        /*保存企业证照信息1*/
        licence.setLicenceCode(tEnterpriseLicence.getLicenceCode());
        licence.setLicenceImg(tEnterpriseLicence.getLicenceImg());
        licence.setLicenceTimg(tEnterpriseLicence.getLicenceTimg());
        licence.setGrantDate(tEnterpriseLicence.getGrantDate());
        licence.setInvalidDate(tEnterpriseLicence.getInvalidDate());
        licence.setType(tEnterpriseLicence.getLicenceType());
        licence.setDescribed(tEnterpriseLicence.getDescribed());
        tSupplierLicenceMapper.insertSelective(licence);
        /*保存企业证照信息2*/
        licence.setLicenceCode(tEnterpriseLicence.getLicenceCode2());
        licence.setLicenceImg(tEnterpriseLicence.getLicenceImg2());
        licence.setLicenceTimg(tEnterpriseLicence.getLicenceTimg2());
        licence.setGrantDate(tEnterpriseLicence.getGrantDate2());
        licence.setInvalidDate(tEnterpriseLicence.getInvalidDate2());
        licence.setType(tEnterpriseLicence.getLicenceType2());
        licence.setDescribed(tEnterpriseLicence.getDescribed2());
        tSupplierLicenceMapper.insertSelective(licence);
	}

	/**
	 * 供应商修改保存
	 * @param tSupplier
	 * @param tEnterpriseLicence
	 */
	public void editSupplier(TSupplier tSupplier, TenterpriseLicence2 tEnterpriseLicence) {
		/*企业证照基础信息*/
		TSupplierLicence licence = new TSupplierLicence();
        /*修改企业证照信息1*/
        licence.setId(tEnterpriseLicence.getLicenceId());
        licence.setLicenceCode(tEnterpriseLicence.getLicenceCode());
        licence.setLicenceImg(tEnterpriseLicence.getLicenceImg());
        licence.setLicenceTimg(tEnterpriseLicence.getLicenceTimg());
        licence.setGrantDate(tEnterpriseLicence.getGrantDate());
        licence.setInvalidDate(tEnterpriseLicence.getInvalidDate());
        licence.setType(tEnterpriseLicence.getLicenceType());
        licence.setDescribed(tEnterpriseLicence.getDescribed());
        tSupplierLicenceMapper.updateByPrimaryKeySelective(licence);
        /*修改企业证照信息2*/
        licence.setId(tEnterpriseLicence.getLicenceId2());
        licence.setLicenceCode(tEnterpriseLicence.getLicenceCode2());
        licence.setLicenceImg(tEnterpriseLicence.getLicenceImg2());
        licence.setLicenceTimg(tEnterpriseLicence.getLicenceTimg2());
        licence.setGrantDate(tEnterpriseLicence.getGrantDate2());
        licence.setInvalidDate(tEnterpriseLicence.getInvalidDate2());
        licence.setType(tEnterpriseLicence.getLicenceType2());
        licence.setDescribed(tEnterpriseLicence.getDescribed2());
        tSupplierLicenceMapper.updateByPrimaryKeySelective(licence);
        /*修改供应商信息*/
        tSupplierMapper.updateByPrimaryKeySelective(tSupplier);
	}
	
	/**
	 * 供应商删除
	 * @param id
	 */
	@Transactional(rollbackFor=Exception.class)
	public void supplierDel(Integer id) {
		try {
			TSupplier tSupplier = new TSupplier();
			tSupplier.setId(id);
			tSupplier.setDeleteFlag("1");
			tSupplierMapper.updateByPrimaryKeySelective(tSupplier);
		} catch (Exception e) {
            logger.error("com.yssoft.service.core.SupplierService.supplierDel删除供应商报错："+e.getMessage());
        }
	}

	public Map<String, Object> findOne(Integer id) {
		Map<String, Object> map = tSupplierMapper.findOne(id);
		return map;
	}

	/**
	 * 查看手机号是否被注册
	 * @param phone
	 * @param request
	 * @return
	 */
	public int supplierIsPhone(String phone, HttpServletRequest request) {
		Map<String, Object> param = new HashMap<>();
		param.put("phone", phone);
		param.put("deleteFlag", "0");
		param.put("tEId", AppUserHelper.getCurrentUserId(request));
		List<Map<String, Object>> list = tSupplierMapper.supplierIsPhone(param);
		return list.size();
	}
	
	/**
	 * 图片上传
	 * @param mFile
	 * @return
	 */
	public Map<String, Object> compressUploadedFile(MultipartFile mFile) {
		Map<String, Object> fileNames = UploadFile.compressUploadedFile(mFile);
		return fileNames;
	}
}
